import http from '@/common/http';
import { Dialog } from 'mand-mobile';
import { GET_BANNERS, GET_POP_ADS } from './actions.types';
import { GET_BANNERS_SUCCESS, GET_POPUP_SUCCESS } from './mutations.types';

const state = {
  banners: [],
  popup: {}
};
const getters = {
  banners(state) {
    return state.banners;
  },
  popup(state) {
    return state.popup;
  }
};
const actions = {
  [GET_BANNERS](context, params) {
    let data = {
      position: '1'
    };
    return new Promise((resolve, reject) => {
      http.post('pakj/adv/getAdvListByPosition', data).then(res => {
        if (res.data.code === '000000') {
          context.commit(GET_BANNERS_SUCCESS, res.data.data);
          resolve(res.data.data);
        } else {
          Dialog.alert({
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  [GET_POP_ADS](context, params) {
    let data = {
      position: '2'
    };
    return new Promise((resolve, reject) => {
      http.post('pakj/adv/getAdvListByPosition', data).then(res => {
        if (res.data.code === '000000') {
          context.commit(GET_POPUP_SUCCESS, res.data.data);
          resolve(res.data.data);
        } else {
          Dialog.alert({
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  }
};
const mutations = {
  [GET_BANNERS_SUCCESS](state, data) {
    state.banners = data;
  },
  [GET_POPUP_SUCCESS](state, data) {
    state.popup = data;
  }
};
export default {
  state,
  actions,
  getters,
  mutations
};
